package com.itboye.jmsfhz.app.ui.me.personsettings;

import java.util.Observable;
import java.util.Observer;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.ui.main.MainPresenter;
import com.itboye.jmsfhz.data.presenter.GetYanzhengMaPresenter;
import com.itboye.jmsfhz.data.repository.YanzhengmaRespository;

public class ActivityUpdatePwdByPhone extends BaseActivity implements
		OnClickListener, Observer {

	EditText input_bindphone, input_newpwd, input_yzm;
	Button btn_submit;
	TextView getYzm;
	private String newPwd;
	MainPresenter mainPresenter;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	private String uid;
	private String code;
	private String phone;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_updatepwdbypphone;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		mainPresenter = new MainPresenter(this);
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.btn_submit) {
			if(phone==null){
				ByAlert.alert("请输入手机号码");
				return ;
			}
			code=input_yzm.getText().toString().trim();
			if(code==null){
				ByAlert.alert("请输入验证码");
				return ;
			}
			newPwd=input_newpwd.getText().toString().trim();
			if(newPwd==null){
				ByAlert.alert("请输入新密码");
				return ;
			}
			mainPresenter.updatePasswordByTelephone(code, newPwd,phone );
		}else if(v.getId() == R.id.getYzm){
			phone=input_bindphone.getText().toString().trim();
			getYanzhengMaPresenter.getVerificationCode(phone, 2+"");
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaSuccessEvent) {
				ByAlert.alert(entity.getData() + "");
				input_yzm.setText(entity.getData()+"");
			} else if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaFailEvent) {
				ByAlert.alert(entity.getData() + "");
			} else if (entity.getEventType() == MainPresenter.UpdatePwdByPhoneSuccessEvent) {
				ByAlert.alert(entity.getData() + "");
				finish();
			} else if (entity.getEventType() == MainPresenter.UpdatePwdByPhoneFailureEvent) {
				ByAlert.alert(entity.getData() + "");
			}
		}
	}

}
